#include "inputdialog.h"
#include <QInputDialog>
#include <QStringList> // getItem需要QStringList

InputDialog::InputDialog(QWidget *parent)
    : QDialog(parent)
{
    nameLabel = new QLabel(tr("Name:"));
    showNameLabel = new QLabel(tr("Tim"));
    showNameLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    nameBtn = new QPushButton(tr("Change name"));
    sexLabel = new QLabel(tr("Sex"));
    showSexLabel = new QLabel(tr("male"));
    showSexLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    sexBtn = new QPushButton(tr("Change sex"));

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(nameLabel, 0, 0);
    mainLayout->addWidget(showNameLabel, 0, 1);
    mainLayout->addWidget(nameBtn, 0, 2);
    mainLayout->addWidget(sexLabel, 1, 0);
    mainLayout->addWidget(showSexLabel, 1, 1);
    mainLayout->addWidget(sexBtn, 1, 2);
    mainLayout->setMargin(10);
    mainLayout->setSpacing(15);

    connect(nameBtn, SIGNAL(clicked()), this, SLOT(showName()));
    connect(sexBtn, SIGNAL(clicked()), this, SLOT(showSex()));
}

InputDialog::~InputDialog()
{

}

void InputDialog::showName()
{
    bool ok;
    QString n = QInputDialog::getText(this,
                                      tr("标准字符输入对话框"),
                                      tr("请输入姓名："),
                                      QLineEdit::Normal,
                                      showNameLabel->text(),
                                      &ok);
    if(ok && !n.isEmpty())
        showNameLabel->setText(n);

}

void InputDialog::showSex()
{
    QStringList SexItems;
    SexItems<<tr("male")<<tr("female");
    bool ok;
    QString s = QInputDialog::getItem(this,
                                      tr("标准条目选择对话框"),
                                      tr("请输入性别："),
                                      SexItems,
                                      0,
                                      false,
                                      &ok);
    if(ok)
        showSexLabel->setText(s);
}
